//
//  DSTabBarViewController.swift
//  MM
//
//  Created by liujianlong on 2022/8/1.
//

import UIKit
import TCCKit
let offx = CGFloat(16)
let offy = CGFloat(5)
///更新Tab颜色通知
let updateTabColor = "updateTabColor"
class DSTabBarViewController: UITabBarController {
    var item_layer_arr = [CALayer]()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        self.setupColor()
        self.setupViewController()
        self.selectedIndex = 0
        NotifyTool.add(self, #selector(updateTabColorAction), updateTabColor)
    }
    
    func setupColor()
    {
        self.tabBar.tintColor = "#FFFFFF".color()
        self.tabBar.unselectedItemTintColor = "#000000".color()
        let bgcolor = "#161718".color()
        if #available(iOS 13, *) {
            let appearance = self.tabBar.standardAppearance.copy()
            //appearance.shadowImage = UIImage.withColor(bgcolor)///设置阴影
            appearance.shadowColor = bgcolor/////设置阴影
            appearance.backgroundImage = UIImage(named: "bg_tabbar")
            appearance.backgroundColor = "#FFFFFF".color(0.0)
            appearance.backgroundEffect = nil
            self.tabBar.standardAppearance = appearance
            if #available(iOS 15.0, *) {
                ///用这个方法的，这个一定要加，否则15.0系统下会出问题，一滑动tabbar就变透明!!!
                self.tabBar.scrollEdgeAppearance = appearance
            } else {}
        }
    }
    
    func setupViewController()
    {
        let controllers = [SumViewController(), CrossViewController(),GainViewController(),EQViewController()]
        let titles = ["SUMMING","CROSSOVER","GAIN","OUTPUT EQ"]
        let imgs = ["summing","cross","gain","eq"]
        let selectImgs = ["summing_select","cross_select","gain_select","eq_select"]
        for i in 0..<controllers.count{
            let vc = controllers[i]
            vc.tabBarItem = UITabBarItem()
            vc.title = titles[i]
            vc.tabBarItem.title = ""
            //             vc.tabBarItem.image = UIImage(named:imgs[i])!
            //            vc.tabBarItem.selectedImage = UIImage(named:selectImgs[i])!
            vc.tabBarItem.image = UIImage(named:imgs[i])?.withRenderingMode(i == 0 || i == 2 ? .alwaysOriginal :.alwaysTemplate)
            vc.tabBarItem.selectedImage = UIImage(named:selectImgs[i])?.withRenderingMode(.alwaysOriginal)
            vc.tabBarItem.imageInsets = UIEdgeInsets(top: -3, left: -3, bottom: -3, right: -3)
            let nav = DSNavigationController(rootViewController:vc)
            nav.navigationBar.isHidden = false
            self.addChild(nav)
        }
    }
    
    @objc func updateTabColorAction(){
        let imgs = ["summing","cross","gain","eq"]
        for i in 0..<imgs.count{
            guard let vc = (self.viewControllers?[i] as? DSNavigationController)?.topViewController as? DSViewController else{ return}
            vc.tabBarItem.image = UIImage(named:imgs[i])?.withRenderingMode(.alwaysOriginal)
        }
    }
    
}

// MARK: - UITabBarControllerDelegate
extension DSTabBarViewController: UITabBarControllerDelegate
{
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        guard let vc = (viewController as? DSNavigationController)?.topViewController as? DSViewController else{ return false}
        if APP.pwd == ""{
            if vc.className == CrossViewController.className || vc.className == EQViewController.className{
                //CLHUD("Please enter your password to unlock the function",type:.info, callBack: nil)
                return false
            }
        }
        return true
    }
    
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        
    }
}
